package com.arcsinw.bingeveryday;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class SimpleImageView extends ImageView {
    public SimpleImageView(Context context) {
        super(context);
    }

    public SimpleImageView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public SimpleImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public SimpleImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    public static final int GET_DATA_SUCCESS = 1;
    public static final int NETWORK_ERROR = 2;
    public static final int SERVER_ERROR = 3;

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message message){
            switch(message.what){
                case GET_DATA_SUCCESS:
                    Bitmap bitmap = (Bitmap)message.obj;
                    setImageBitmap(bitmap);
                    break;
                case NETWORK_ERROR:
                    Toast.makeText(getContext(), "Network error", Toast.LENGTH_SHORT).show();
                case SERVER_ERROR:
                    Toast.makeText(getContext(), "Server error", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };

    /**
     * set ImageView's source (support network uri)
     * @param uriString image's uri
     */
    public void setImageUriEx(final String uriString){
        new Thread(){
            @Override
            public void run(){
                Bitmap bitmap;
                try {
                    bitmap = download(uriString);
                    Message message = Message.obtain();
                    message.obj = bitmap;
                    message.what = GET_DATA_SUCCESS;
                    handler.sendMessage(message);
                }
                catch (MalformedURLException e){
                    e.printStackTrace();
                }
                catch (IOException e) {
                    handler.sendEmptyMessage(NETWORK_ERROR);
                    e.printStackTrace();
                }
            }
        }.start();
    }

    /**
     * set ImageView's source (support network uri)
     * @param url
     */
    public void setImageUriEx(final URL url){
        new Thread(){
            @Override
            public void run(){
                try{
                    HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
                    urlConnection.setRequestMethod("GET");
                    urlConnection.setConnectTimeout(10000);
                    int responseCode = urlConnection.getResponseCode();
                    if (responseCode == 200){
                        InputStream inputStream = urlConnection.getInputStream();
                        Bitmap bitmap = BitmapFactory.decodeStream(inputStream);

                        Message message = Message.obtain();
                        message.obj = bitmap;
                        message.what = GET_DATA_SUCCESS;
                        handler.sendMessage(message);
                    }
                    else{
                        handler.sendEmptyMessage(SERVER_ERROR);
                    }

                } catch (MalformedURLException e) {

                    e.printStackTrace();
                } catch (IOException e) {
                    handler.sendEmptyMessage(NETWORK_ERROR);
                    e.printStackTrace();
                }
            }
        };
    }

    /**
     * download image from network
     * @param uriString
     * @return
     */
    public Bitmap download(String uriString) throws IOException{
        try{
            URL url = new URL(uriString);
            HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
            urlConnection.setRequestMethod("GET");
            urlConnection.setConnectTimeout(10000);
            int responseCode = urlConnection.getResponseCode();
            if (responseCode == 200){
                InputStream inputStream = urlConnection.getInputStream();
                return BitmapFactory.decodeStream(inputStream);
            }
        } catch (IOException e) {
            throw e;
        }
        return null;
    }
}
